package com.vhans.bapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.vhans.core.annotation.AccessLimit;
import com.vhans.core.annotation.OptLogger;
import com.vhans.core.annotation.VisitLogger;
import com.vhans.core.enums.LikeTypeEnum;
import com.vhans.core.strategy.context.LikeStrategyContext;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.model.vo.RecordSearchVO;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.dto.DeleteDTO;
import com.vhans.db.model.dto.record.RecordDTO;
import com.vhans.db.model.vo.record.*;
import com.vhans.db.service.IBRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.vhans.core.constant.OptTypeConstant.*;

/**
 * 记录控制器
 *
 * @author vhans
 **/
@Api(tags = "记录模块")
@RestController
public class RecordController extends BaseController {

    @Autowired
    private IBRecordService recordService;

    @Autowired
    private LikeStrategyContext likeStrategyContext;

    /**
     * 添加记录
     *
     * @param record 记录信息
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "添加记录")
    @PostMapping("/record/add")
    public Result<?> addRecord(@Validated @RequestBody RecordDTO record) {
        recordService.addRecord(record);
        return Result.success();
    }

    /**
     * 回收或恢复记录
     *
     * @param delete 逻辑删除
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "回收或恢复记录")
    @PutMapping("/record/recycle")
    public Result<?> updateRecordDelete(@Validated @RequestBody DeleteDTO delete) {
        recordService.updateRecordDelete(delete);
        return Result.success();
    }

    /**
     * 修改记录
     *
     * @param record 记录信息
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "修改记录")
    @PutMapping("/record/update")
    public Result<?> updateRecord(@Validated @RequestBody RecordDTO record) {
        recordService.updateRecord(record);
        return Result.success();
    }

    /**
     * 编辑记录
     *
     * @param recordId 记录id
     * @return {@link Result<RecordInfoVO>} 后台记录
     */
    @SaCheckLogin
    @ApiOperation(value = "编辑记录")
    @GetMapping("/record/edit/{recordId}")
    public Result<RecordInfoVO> editRecord(@PathVariable("recordId") Integer recordId) {
        return Result.success(recordService.editRecord(recordId));
    }

    /**
     * 上传记录图片
     *
     * @param file 文件
     * @return {@link Result<String>} 记录图片地址
     */
    @SaCheckLogin
    @ApiOperation(value = "上传记录图片")
    @ApiImplicitParam(name = "file", value = "记录图片", required = true, dataType = "MultipartFile")
    @PostMapping("/recordImage/upload")
    public Result<String> saveRecordImages(@RequestParam("file") MultipartFile file) {
        return Result.success(recordService.saveRecordImages(file));
    }

    /**
     * 上传记录视频
     *
     * @param file 文件
     * @return {@link Result<String>} 记录视频地址
     */
    @SaCheckLogin
    @ApiOperation(value = "上传记录视频")
    @ApiImplicitParam(name = "file", value = "记录视频", required = true, dataType = "MultipartFile")
    @PostMapping("/recordVideo/upload")
    public Result<String> saveRecordVideo(@RequestParam("file") MultipartFile file) {
        return Result.success(recordService.saveRecordVideo(file));
    }

    /**
     * 点赞记录
     *
     * @param recordId 记录id
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "点赞记录")
    @AccessLimit(seconds = 60, maxCount = 3)
    @PostMapping("/record/{recordId}/like")
    public Result<?> likeRecord(@PathVariable("recordId") Integer recordId) {
        likeStrategyContext.executeLikeStrategy(LikeTypeEnum.RECORD, recordId);
        return Result.success();
    }

    /**
     * 收藏记录
     *
     * @param recordId 记录id
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "收藏记录")
    @PostMapping("/record/{recordId}/collect")
    public Result<?> collectRecord(@PathVariable("recordId") Integer recordId) {
        recordService.collectRecord(recordId);
        return Result.success();
    }

    /**
     * 搜索记录
     *
     * @param keyword 关键字
     * @return {@link Result<RecordSearchVO>} 记录列表
     */
    @ApiOperation(value = "搜索记录")
    @GetMapping("/record/search")
    public Result<List<RecordSearchVO>> listRecordsBySearch(String keyword) {
        return Result.success(recordService.listRecordsBySearch(keyword));
    }

    /**
     * 查看记录中心记录列表
     *
     * @param keyword 关键字
     * @return {@link Result<RecordHomeVO>} 记录列表
     */
    @VisitLogger(value = "首页")
    @ApiOperation(value = "查看记录中心记录列表")
    @GetMapping("/record/list")
    public TableDataInfo listRecordHomeVO(String keyword) {
        startPage();
        List<RecordHomeListVO> list = recordService.listRecordHomeVO(keyword);
        return getDataTable(list);
    }

    /**
     * 查看记录
     *
     * @param recordId 记录id
     * @return {@link Result<RecordVO>} 首页记录
     */
    @ApiOperation(value = "查看记录")
    @GetMapping("/record/{recordId}")
    public Result<RecordHomeVO> getRecordHomeById(@PathVariable("recordId") Integer recordId) {
        return Result.success(recordService.getRecordHomeById(recordId));
    }

    /**
     * 查看推荐记录
     *
     * @return {@link Result<RecordRecommendVO>} 推荐记录
     */
    @ApiOperation(value = "查看推荐记录")
    @GetMapping("/record/recommend")
    public Result<List<RecordRecommendVO>> listRecordRecommendVO() {
        return Result.success(recordService.listRecordRecommendVO());
    }

    /**
     * 查看记录总览
     *
     * @return {@link TableDataInfo} 记录总览列表
     */
    @VisitLogger(value = "总览")
    @ApiOperation(value = "查看记录总览")
    @GetMapping("/record/overview/list")
    public TableDataInfo listRecordOverviewVO() {
        startPage();
        List<RecordOverviewVO> list = recordService.listRecordOverviewVO();
        return getDataTable(list);
    }

}